package com.kyzh.core.utils

import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.kyzh.core.adapters.orientationUtils
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView

fun SampleCoverVideo.setConfig(videoUrl: String, videoBG: String?, position: Int = 0) {
    if (videoBG != null) {
        if (videoBG.isNotEmpty()) {
            val imageView = ImageView(context)
            Glide.with(context).load(videoBG).apply(
                RequestOptions()
                    .transforms(
                        CenterCrop(), RoundedCorners(15)
                    )
            ).into(imageView)
            thumbImageView = imageView
        }
    }
    isNeedShowWifiTip = false
    isLooping = true
    dismissControlTime = 2000
    setIsTouchWiget(true)
    setThumbPlay(true)
    backButton.visibility = View.GONE
    backButton.setOnClickListener {
        GSYVideoManager.backFromWindowFull(context)
    }
    fullscreenButton.setOnClickListener {
        backButton.visibility = View.VISIBLE
        startWindowFullscreen(context, false, false)
    }
    startButton.setOnClickListener {
        when (currentState) {
            GSYVideoView.CURRENT_STATE_PAUSE -> {
                onVideoResume(false)
            }
            GSYVideoView.CURRENT_STATE_PLAYING -> {
                onVideoPause()
            }
            else -> {
                startPlayLogic()
            }
        }
    }
    playPosition = position
    isLockLand = true
    isAutoFullWithSize = true
    isShowFullAnimation = true;
    backButton.visibility = View.GONE;
//    fullscreenButton.setOnClickListener {
//        orientationUtils?.resolveByClick();
//        //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
//        startWindowFullscreen(context, true, true);
//    }
    setVideoAllCallBack(object : GSYSampleCallBack() {
        override fun onPrepared(url: String?, vararg objects: Any?) {
            super.onPrepared(url, *objects)
        }

        override fun onEnterFullscreen(url: String?, vararg objects: Any?) {
            super.onEnterFullscreen(url, *objects)
        }

        override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
            super.onQuitFullscreen(url, *objects)
            backButton.visibility = View.GONE

        }
    })
    setUp(videoUrl, true, "")
}